import React from 'react';

//1.导入页面组件
import Login from "../pages/Login/Login";
import Admin from "../layout/Admin";
import AdminIndex from '../pages/AdminIndex' //后台首页(右侧)
import ProductList from "../pages/Product/ProductList";
import ProductAdd from "../pages/Product/ProductAdd";
import GoodsCateMange from  '../pages/Product/GoodsCateMange';
import AccountList from '../pages/Account/AccountList'
import AccountAdd from '../pages/Account/AccountAdd'
import EditPassWord from "../pages/Account/EditPassWord";
import Page404 from "../pages/Page404";

import GoodsAnalysis from '../pages/Sales/GoodsAnalysis'
import OrderHome from '../pages/Order/OrderHome'
import ShopHome from '../pages/Shop/ShopHome'

import {Navigate} from "react-router";

//2.配置路由数组
//2.1 这是默认情况下，加载的路由
const routes = [
    // 访问/的时候，跳转到 /login
    { path: "/", element: <Navigate to="/login" />, role:['super','normal'],},
    { path: '/login', element:<Login />, role:['super','normal'], },
    { path: '*', element:<Page404/>, role:['super','normal'],},
];

//以下内容，会根据权限进行处理
//2.2 这个是在用户登录后加载的路由，当用户登录成功后，会把这两个合在一起，生成一个新的路由
const dynamic_routes = [
    {
        path: '/admin',
        // element:()=> <Admin />,
        element: <Admin />,
        role:['super','normal'],
        label:"首页",
        children:[
            {
                path: 'home', //后台的首页，其实是 销售统计-》订单统计 菜单
                element: <Navigate to="/admin/index" />,
                role:['super','normal'],
                label:"后台首页", //为了生成菜单名称
                key:"home"//为了生成菜单时点击跳转
            },
            {
                path: 'order_home',
                role:['super','normal'],
                element:<OrderHome />,
                label:"订单管理", //为了生成菜单名称
                key:"order_home"//为了生成菜单时点击跳转
            },
            {
                path:"product_list",
                element:<ProductList />,
                role:['super','normal'],
                label:"商品列表", //为了生成菜单名称
                key:"product_list"//为了生成菜单时点击跳转
            },
            {
                path:"product_add",
                element:<ProductAdd />,
                role:['super','normal'],
                label: "商品添加",
                key:"product_add"
            },
            {
                path:"goods_cate_mange",
                element: <GoodsCateMange />,
                role:['super','normal'],
                label:"分类管理",
                key:"goods_cate_mange",
            },
            {
                path:"shop_home",
                element: <ShopHome /> ,
                role:['super','normal'],
                label: "店铺管理",
                key:"shop_home"
            },
            {
                path:"account_list",
                element:<AccountList />,
                role:['super'], //只有admin角色才可以
                label: "账号列表",
                key:"account_list"
            },
            {
                path:"account_add",
                role:['super'],//只有admin角色才可以
                element:<AccountAdd />,
                label: "添加账号",
                key:"account_add",
            },
            {
                path:"edit_password",
                role:['super','normal'],
                element:<EditPassWord />,
                label: "修改密码",
                key:"edit_password",
            },
            //后台首页（销售统计-订单统计）
            {
                path:"index",
                element:<AdminIndex />,
                role:['super','normal'],
                label: "订单统计",
                key:"index",
            },
            //后台首页（销售统计-商品统计）
            {
                path:"goods_analysis",
                role:['super'],
                element:<GoodsAnalysis />,
                label: "商品统计",
                key:"goods_analysis",
            },
        ]
    }
]

//4.根据用户角色，筛选出能访问的路由数组
// let role = localStorage.getItem('user_role')?localStorage.getItem('user_role'):null;
//定义一个方法，获取角色对应的路由
//role 角色
//arr 路由对象数组
function getUserRoute(role,arr){
    let arr_new = arr.filter((v,k)=>{
        let vv = v.role || []; //角色数组
        if(vv&&vv.includes(role)) {
            // return v;
            // 子路由处理
            console.log("children:",v.children)
            // ?. 可选链运算符号，在访问对象的属性，当不确属性是否存的时候用
            // v?.children == v && v.children 
            // v?.children?.items => v && v.children && v.children.items
            if(v?.children){ 
                let children = getUserRoute(role,v.children) //重新调一次自己
                v['children'] = children;
            }
            return v;
        }
    })
    return arr_new;
}


export {dynamic_routes,getUserRoute}

//3.导出默认路由数组
export default routes;